import React from 'react';
import { Button, Form, message, Table } from 'antd';
import request from '@/utils/request';
import { VendorSelect, EmpSelect } from '@/components/FormItemSelect';

const apiURL = '/api/services/app/POHeader/';
class CreatePOForm extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
      dataSource: [],
      poNum: 0
    };
  }
  formRef: any = React.createRef();
  onFinish = (values: any) => {
    var data = values.model;
    data.termsCode = 'HDFK'
    data.shipViaCode = '汽运'
    data.entryPerson = 'admin'
    data.currencyCode = 'RMB'
    data.poType = 'ipo'
    data.items = this.props.data
    this.setState({ loading: true });
    var act = 'CreateBySugg';
    request(apiURL + act, {
      method: 'POST',
      data
    }).then((response) => {
      if (response) {
        if (response.success) {
          message.success('创建成功！');
          this.setState({ poNum: response.result.poNum })
        }
        this.setState({ loading: false });
      }
    });
  };
  render() {
    const layout = {
      labelCol: { span: 5 },
      wrapperCol: { span: 19 },
    };
    const columns = [
      {
        title: '物料编码',
        dataIndex: 'partNum',
        width: 100,
      },
      {
        title: '描述',
        dataIndex: 'lineDesc',
        ellipsis: true
      },
      {
        title: '数量',
        dataIndex: 'xRelQty',
        width: 100,
      },
    ];
    return (
      <>
        <Form {...layout} ref={this.formRef} onFinish={this.onFinish}>
          <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 5 }}>
            <Button type="link">
              采购订单号：{this.state.poNum}
            </Button>
          </Form.Item>
          <EmpSelect
            required={true}
            width={200}
            name={['model', 'buyerID']}
            label="采购员"
          />
          <VendorSelect
            required={true}
            showSearch={true}
            width={200}
            name={['model', 'vendorNum']}
            label="供应商"
          />
          <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 5 }}>
            <Button type="primary" htmlType="submit" loading={this.state.loading}>
              确定创建
            </Button>
          </Form.Item>
        </Form>
        <Table
          rowKey="id"
          size="small"
          columns={columns}
          dataSource={this.props.data}
          pagination={false}
          scroll={{ x: 600 }}
          bordered
        />
      </>
    );
  }
}
export default CreatePOForm;
